package com.green.common;

import java.io.ByteArrayOutputStream;
import java.io.DataOutput;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.Serializable;

public class BOSUuid implements Serializable {

	private static final long serialVersionUID = -65533L;
	private transient Uuid uuid;
	private transient BOSObjectType type;
	 private transient byte encoding;//encoding默认为1
	
	private BOSUuid(Uuid uuid, BOSObjectType type) {
	    	  
	    this.encoding = 1;
	    this.uuid = uuid;
	    this.type = type;
	  }
	
	public static BOSUuid create(String typeString)
	  {
	    assert ((typeString.length() == 8) || (typeString.length() == 4));

	    return create(BOSObjectType.create(typeString));
	  }
	
	 public static BOSUuid create(BOSObjectType type)
	  {
	    assert (type != null);
	    return new BOSUuid(Uuid.create(), type);
	  }
	
	public String toString()
	  {
	    if (this.encoding == 1) {
	      ByteArrayOutputStream baos = new ByteArrayOutputStream(28);
	      
	      DataOutput out = new DataOutputStream(baos);
	     
	        try {
				this.uuid.write(out);
				this.type.write(out);
	        } catch (IOException e) {
				e.printStackTrace();
			}
	      return Base64Encoder.byteArrayToBase64(baos.toByteArray());
	    }if (this.encoding == 0) {
	      return this.uuid.toString() + this.type.toString();
	    }
	    throw new AssertionError("unhandled encoding.");
	  }
}
